//
//  Pet_SignInViewController.swift
//  PetApp
//
//  Created by mac on 2023/9/18.
//  Copyright © 2023 Pet_. All rights reserved.
//

import UIKit

class Pet_SignInViewController: BasicViewController {
    
    let img = UIImageView.initImgView(img: "topBack")
    let txtfiled1 = UITextField.init(frame: .zero)
    let txtfiled2 = UITextField.init(frame: .zero)
    let titleL = UILabel.initV(.setTypeFace(24.Fit,type: .extraBold), text: "Register", textColor: .black)
    let tipL = UILabel.initV(.setTypeFace(16.Fit,type: .medium), text: "Create your new account", textColor: .black)

    private lazy var doneButton: UIButton = {
        let button = UIButton(type: .custom)
        button.titleLabel?.font = .setTypeFace(18.Fit,type: .bold)
        button.setTitleColor(.black, for: .normal)
        button.setTitle("LOGIN", for: .normal)
        button.backgroundColor = mainColor
        button.layer.cornerRadius = 25
        button.tag = 100
        return button
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.subviews {
            img.subviews {
                titleL
                tipL
            }
            txtfiled1
            txtfiled2
            doneButton
        }
        
        img.top(0).left(0).right(0).height(200.Fit)
        img.centerHorizontally()
        titleL.left(15.Fit).right(15.Fit).height(30.Fit)
        titleL.textAlignment = .center
        titleL.centerVertically()
        
        tipL.left(15.Fit).right(15.Fit).height(25.Fit)
        tipL.textAlignment = .center
        tipL.Top == titleL.Bottom

        txtfiled1.left(30.Fit).right(30.Fit).height(50.Fit)
        let attriText = NSMutableAttributedString.init(string: "Please enter your E-mail")
        attriText.addAttributes([NSAttributedString.Key.foregroundColor: mainColor], range: NSRange(location: 0, length: attriText.string.count))
        txtfiled1.attributedPlaceholder = attriText
        txtfiled1.textColor = mainColor
        txtfiled1.backgroundColor = .init(white: 1, alpha: 0.15)
        txtfiled1.font = .setTypeFace(16.Fit,type: .bold)
        txtfiled1.Top == img.Bottom + 35.Fit
        txtfiled1.setAllCorners(corner: 25.Fit)
        
        let v1 = UIView.init(frame: .init(x: 0, y: 0, width: 60, height: 50))
        let img1 = UIImageView.initImgView(img: "email")
        img1.frame = .init(x: 15, y: 5, width: 40, height: 40)
        v1.addSubview(img1)
        txtfiled1.leftView = v1
        txtfiled1.leftViewMode = .always

        
        txtfiled2.left(30.Fit).right(30.Fit).height(50.Fit)
        txtfiled2.Top == txtfiled1.Bottom + 40.Fit
        txtfiled2.setAllCorners(corner: 25.Fit)
        
        let attriText1 = NSMutableAttributedString.init(string: "Please enter your password")
        attriText1.addAttributes([NSAttributedString.Key.foregroundColor: mainColor], range: NSRange(location: 0, length: attriText1.string.count))
        txtfiled2.attributedPlaceholder = attriText1
        txtfiled2.textColor = mainColor
        txtfiled2.font = .setTypeFace(16.Fit,type: .bold)
        txtfiled2.isSecureTextEntry = true
        txtfiled2.backgroundColor = .init(white: 1, alpha: 0.15)
        let v2 = UIView.init(frame: .init(x: 0, y: 0, width: 60, height: 50))
        let img2 = UIImageView.initImgView(img: "pwd")
        img2.frame = .init(x: 15, y: 5, width: 40, height: 40)
        v2.addSubview(img2)
        txtfiled2.leftView = v2
        txtfiled2.leftViewMode = .always

        doneButton.left(30.Fit).right(30.Fit).height(50).bottom(TabbarHeight)
        doneButton.addAct {
            // 登录
            guard self.txtfiled1.text?.validateEmail() == true else {
                Toast_View_Tool.show(type: .error,text: "Invalid email address.")
                return
            }
            guard self.txtfiled2.text?.validatePassword() == true else{
                Toast_View_Tool.show(type: .error,text: "The password should be 6-14 digits.")
                return
            }
            // 登录接口
            RT.LogininWith(emailNum: self.txtfiled1.text ?? "", Password: self.txtfiled2.text ?? "") { info in
                if info.xz?.count == 0
                {
                    let vc = Pet_LoginInfoViewController()
                    self.navigationController?.pushViewController(vc, animated: true)
                }else
                {
                    let str = GetInfo(key: "UserInfo") as! NSDictionary
                    let jsonData = InfoUtil.initInfo.deserialize(from: str)
                    USEINFOMODEL = jsonData!
                    let vc = Pet_TabbarViewController()
                    UIApplication.shared.keyWindow?.rootViewController = vc
                }
            }

        }
        
    }

}
